/*************************************************************************
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *
 *                COPYRIGHT (C) HONGLING CAPITAL CORPORATION 2012
 *    ALL RIGHTS RESERVED BY HONGLING CAPITAL CORPORATION. THIS PROGRAM
 * MUST BE USED  SOLELY FOR THE PURPOSE FOR WHICH IT WAS FURNISHED BY
 * HONGLING CAPITAL CORPORATION. NO PART OF THIS PROGRAM MAY BE REPRODUCED
 * OR DISCLOSED TO OTHERS,IN ANY FORM, WITHOUT THE PRIOR WRITTEN
 * PERMISSION OF HONGLING CAPITAL CORPORATION. USE OF COPYRIGHT NOTICE
 DOES NOT EVIDENCE PUBLICATION OF THE PROGRAM.
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *************************************************************************/

package com.springboot.controller;

import com.springboot.common.annotation.RateLimit;
import com.springboot.common.enums.LimitType;
import com.springboot.common.jms.QueueMessage;
import com.springboot.entity.User;
import com.springboot.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试核心业务类。
 *
 * @author hongchao zhao at 2019-4-19 10:55
 */
@Controller
public class IndexController {

    private static Logger LOG = LoggerFactory.getLogger(IndexController.class);

    @Resource
    private UserService userService;
    @Resource
    private QueueMessage queueMessage;

    private Integer jspCount = 0;
    private Integer htmlCount = 0;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, Model model) {
        LOG.debug("进入JSP请求模板。。。");
        jspCount++;
        int id = Integer.parseInt(request.getParameter("id"));
        User user = userService.getUserById(id);
        model.addAttribute("msg", "欢迎【" + user.getUserName() + "】来到Spring Boot的领域！");
        Map<String, Object> params = new HashMap<>();
        params.put("msg", "我就是传说中的【JSP" + jspCount + "】。");
        queueMessage.sendMapMessage(QueueMessage.UPDATE_TOPIC_READ_REPLY_COUNT, params);
        return "jsp/index";
    }

    @RequestMapping("/index2")
    public String index2(HttpServletRequest request, Model model) {
        LOG.debug("进入HTML请求模板。。。");
        htmlCount++;
        int id = Integer.parseInt(request.getParameter("id"));
        User user = userService.getUserById(id);
        model.addAttribute("msg", "欢迎【" + user.getUserName() + "】来到Spring Boot的领域！");
        Map<String, Object> params = new HashMap<>();
        params.put("msg", "我就是传说中的【HTML" + htmlCount + "】。");
        queueMessage.sendMapMessage(QueueMessage.UPDATE_TOPIC_READ_REPLY_COUNT, params);
        return "html/index";
    }

    @RequestMapping("/index3")
    //限制方法入口每秒最多访问10次
    @RateLimit(key = "testKey", time = 1, count = 10 ,limitType = LimitType.IP)
    public String index3(HttpServletRequest request, Model model) {
        LOG.debug("进入index3请求模板。。。");

        return "html/index";
    }

    @RequestMapping("/index4")
    //限制方法入口每秒最多访问10次
    public String index4(HttpServletRequest request, Model model) {
        LOG.debug("进入index4请求模板。。。");

        return "html/chatRoom/chatRoomTest";
    }

}
